﻿using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Util;
using Kingdee.BOS.Web.DynamicForm;
using Kingdee.BOS.Core.Metadata;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.K3.MFG.ServiceHelper;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core;
using static Kingdee.BOS.Core.Metadata.FlexType;
using Kingdee.BOS.Orm;
using Kingdee.K3.Core.SCM;
using Kingdee.BOS;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.App.Core;

namespace RK.Dz_ConvertBill.Ronma
{
    [Description("自定义单据转换_润马WSG"), HotUpdate]
    public class DzConvertBill : AbstractConvertPlugIn
    {
        string text = string.Empty;
        string text2 = string.Empty;
        string empty = string.Empty;
        string empty2 = string.Empty;
        string empty3 = string.Empty;
        string text3 = string.Empty;
        string empty4 = string.Empty;
        string text4 = string.Empty;
        string text5 = string.Empty;
        string text6 = string.Empty;
        string text7 = "FEntity";
        public override void AfterConvert(AfterConvertEventArgs e)
        {
            base.AfterConvert(e);
            if (!this.Option.GetVariables().ContainsKey("MESParamters"))
            {
                return;
            }
            var array = e.Result.FindByEntityKey("FBillHead");
            IBillView val = CreateBillView(e.TargetBusinessInfo.GetForm().Id);
            Dictionary<string, object> dictionary = this.Option.GetVariables()["MESParamters"] as Dictionary<string, object>;
            int num = 1;
            SetName(e.SourceBusinessInfo.GetForm().Id, e.TargetBusinessInfo.GetForm().Id, num);
            var array2 = array;
            int num2 = 0;
            int num3 = 0;
            SetCustVaue(array2, val, dictionary, e, num, num2);
            ValidateRows(dictionary, num3, num2);
        }

        private void ValidateRows(Dictionary<string, object> dictionary, int num3, int num2)
        {
            if (dictionary.ContainsKey("validateRows") && dictionary["validateRows"].ToString() == "true")
            {
                Dictionary<string, object> dictionary10 = dictionary["FEntity"] as Dictionary<string, object>;
                foreach (KeyValuePair<string, object> item7 in dictionary10)
                {
                    num3 += (item7.Value as Dictionary<string, object>).Count;
                }
                if (num2 != num3)
                {
                    throw new KDException("ErrorCode", "请求行数与生成单据行数不一致");
                }
            }
        }

        private void SetCustVaue(ExtendedDataEntity[] array2, IBillView val, Dictionary<string, object> dictionary, AfterConvertEventArgs e, int num, int num2)
        {
            foreach (var val2 in array2)
            {
                val.Model.DataObject = val2.DataEntity;
                if (dictionary.ContainsKey("FBillHead") && (dictionary["FBillHead"] as Dictionary<string, object>).Count > 0)
                {
                    foreach (var item2 in dictionary["FBillHead"] as Dictionary<string, object>)
                    {
                        if (item2.Value is Dictionary<string, object>)
                        {
                            Dictionary<string, object> dictionary2 = item2.Value as Dictionary<string, object>;
                            if (dictionary2.ContainsKey("FBarItemClick"))
                            {
                                ((DynamicWebFormView)val).MainBarItemClick(item2.Key);
                            }
                            else if (dictionary2.ContainsKey("FNumber"))
                            {
                                val.Model.SetItemValueByNumber(item2.Key, Convert.ToString(dictionary2["FNumber"]), 0);
                                val.InvokeFieldUpdateService(item2.Key, 0);
                            }
                        }
                        else
                        {
                            val.Model.SetValue(item2.Key, Convert.ToString(item2.Value), 0);
                            val.InvokeFieldUpdateService(item2.Key, 0);
                        }
                    }
                }
                RelatedFlexGroupField val3 = val.BusinessInfo.GetField(text4) as RelatedFlexGroupField;
                if (!val3.IsNullOrEmptyOrWhiteSpace())
                {
                    val3.FlexDisplayFormat = (Format)2;
                }
                DynamicObjectCollection val4 = val.Model.DataObject[text] as DynamicObjectCollection;
                Entity entity = val.BusinessInfo.GetEntity("FEntity");
                RelatedFlexGroupField val5 = val.BusinessInfo.GetField("FAuxPropId") as RelatedFlexGroupField;
                Dictionary<int, int> dictionary3 = new Dictionary<int, int>();
                if (dictionary.ContainsKey("FEntity") && (dictionary["FEntity"] as Dictionary<string, object>).Count > 0)
                {
                    Dictionary<string, int> dictionary4 = new Dictionary<string, int>();
                    SelectKey(dictionary4, val4, val);
                    int num5 = val4.Count;
                    int count = val4.Count;
                    bool flag = false;
                    for (int j = 0; j < count; j++)
                    {
                        Dictionary<string, object> dictionary5 = dictionary["FEntity"] as Dictionary<string, object>;
                        string key2 = val4[j][text2].ToString();
                        if (!dictionary5.ContainsKey(key2) || (dictionary5[key2] as Dictionary<string, object>).Count <= 0)
                        {
                            continue;
                        }
                        Dictionary<string, object> dictionary6 = dictionary5[key2] as Dictionary<string, object>;
                        for (int k = 0; k < dictionary6.Count; k++)
                        {
                            Dictionary<string, object> dictionary7 = dictionary6[k.ToString()] as Dictionary<string, object>;
                            int num6;
                            if (k > 0)
                            {
                                if (dictionary7.ContainsKey("FProductType") && dictionary7["FProductType"].ToString() == "2" && (StringUtils.EqualsIgnoreCase(e.TargetBusinessInfo.GetForm().Id, "PRD_INSTOCK") || StringUtils.EqualsIgnoreCase(e.TargetBusinessInfo.GetForm().Id, "PRD_MORPT")))
                                {
                                    val.Model.CreateNewEntryRow(text7);
                                    flag = true;
                                }
                                else
                                {
                                    dictionary3.Remove(j);
                                    if (num == 2)
                                    {
                                        var item = val4[j].Clone(false, true);
                                        val4.Add((DynamicObject)item);
                                    }
                                    else
                                    {
                                        AssociatedCopyRowUtil.CopyRow(val, text7, j, num5, false, null, true, null);
                                    }
                                    flag = false;
                                }
                                num6 = num5;
                                num5++;
                            }
                            else if (dictionary7.ContainsKey("FProductType") && dictionary7["FProductType"].ToString() == "2" && (StringUtils.EqualsIgnoreCase(e.TargetBusinessInfo.GetForm().Id, "PRD_INSTOCK") || StringUtils.EqualsIgnoreCase(e.TargetBusinessInfo.GetForm().Id, "PRD_MORPT")))
                            {
                                dictionary3.Add(j, 1);
                                val.Model.CreateNewEntryRow(text7);
                                flag = true;
                                num6 = num5;
                                num5++;
                            }
                            else
                            {
                                num6 = j;
                            }
                            if (flag)
                            {
                                try
                                {
                                    Dictionary<string, object> dictionary8 = dictionary7["FMaterialId"] as Dictionary<string, object>;
                                    if (!dictionary8["FNumber"].IsNullOrEmptyOrWhiteSpace())
                                    {
                                        val.Model.SetItemValueByNumber("FMaterialId", dictionary8["FNumber"].ToString(), num6);
                                        val.InvokeFieldUpdateService("FMaterialId", num6);
                                    }
                                }
                                catch
                                {
                                    throw new KDException("ErrorCode", "没有找到料号信息");
                                }
                            }
                            foreach (var item3 in dictionary7)
                            {
                                if (Convert.ToBoolean(((val4[num6][text6] as DynamicObject)["MaterialStock"] as DynamicObjectCollection)[0]["IsBatchManage"]) || !StringUtils.EqualsIgnoreCase(item3.Key, text5))
                                {
                                    if (!val4[num6][text3].IsNullOrEmptyOrWhiteSpace() && Convert.ToBoolean((val4[num6][text3] as DynamicObject)["IsOpenLocation"]) && StringUtils.EqualsIgnoreCase(item3.Key, text4))
                                    {
                                        if (item3.Value is Dictionary<string, object> && (item3.Value as Dictionary<string, object>).ContainsKey("FNumber") && (item3.Value as Dictionary<string, object>)["FNumber"] is Dictionary<string, object>)
                                        {
                                            foreach (var item4 in (item3.Value as Dictionary<string, object>)["FNumber"] as Dictionary<string, object>)
                                            {
                                                string cangWeiZhiJi = GetCangWeiZhiJi("BD_FLEXVALUES", item4.Key);
                                                if (!cangWeiZhiJi.IsNullOrEmptyOrWhiteSpace())
                                                {
                                                    val.Model.SetItemValueByID(text4, cangWeiZhiJi, num6);
                                                    val.InvokeFieldUpdateService(text4, num6);
                                                    val.Model.SetItemValueByNumber($"$${text4}__FF{cangWeiZhiJi}", Convert.ToString(item4.Value), num6);
                                                    val.InvokeFieldUpdateService($"$${text4}__FF{cangWeiZhiJi}", num6);
                                                }
                                            }
                                        }
                                    }
                                    else if (item3.Value is Dictionary<string, object>)
                                    {
                                        Dictionary<string, object> dictionary9 = item3.Value as Dictionary<string, object>;
                                        if (item3.Key == "FAuxPropId")
                                        {
                                            var val6 = new DynamicObject(val5.RefFormDynamicObjectType);
                                            val5.RefIDDynamicProperty.SetValue(val4[num6], 0);
                                            val5.DynamicProperty.SetValue(val4[num6], val6);
                                            foreach (var item5 in dictionary9)
                                            {
                                                if (!item5.Value.IsNullOrEmptyOrWhiteSpace())
                                                {
                                                    Field field = val5.RelateFlexBusinessInfo.GetField(item5.Key);
                                                    if (field is BaseDataField)
                                                    {
                                                        BaseDataField val7 = field as BaseDataField;
                                                        DynamicObject dynamicObjectByFormIdAndId = GetDynamicObjectByFormIdAndId(this.Context, "BOS_ASSISTANTDATA_DETAIL", null, "FNumber = '" + item5.Value?.ToString() + "'");
                                                        if (dynamicObjectByFormIdAndId.IsNullOrEmptyOrWhiteSpace())
                                                        {
                                                            throw new KDException("ErrorCode", "请求的辅助属性不存在：" + item5.Value?.ToString());
                                                        }
                                                        var array3 = BusinessDataServiceHelper.LoadFromCache(this.Context, new object[1] { dynamicObjectByFormIdAndId["id"] }, val7.RefFormDynamicObjectType);
                                                        val7.RefIDDynamicProperty.SetValue(val6, dynamicObjectByFormIdAndId["id"]);
                                                        val7.DynamicProperty.SetValue(val6, array3[0]);
                                                    }
                                                    else if (!field.IsNullOrEmptyOrWhiteSpace())
                                                    {
                                                        field.DynamicProperty.SetValue(val6, item5.Value);
                                                    }
                                                }
                                            }
                                            val.InvokeFieldUpdateService(item3.Key, num6);
                                        }
                                        else if (dictionary9.ContainsKey("FBarItemClick"))
                                        {
                                            ((DynamicWebFormView)val).EntryBarItemClick(text7, item3.Key);
                                        }
                                        else if (dictionary9.ContainsKey("FNumber") && item3.Key != "FMaterialId" && !dictionary9["FNumber"].IsNullOrEmptyOrWhiteSpace())
                                        {
                                            val.Model.SetItemValueByNumber(item3.Key, Convert.ToString(dictionary9["FNumber"]), num6);
                                            val.InvokeFieldUpdateService(item3.Key, num6);
                                        }
                                    }
                                    else if (!item3.Value.IsNullOrEmptyOrWhiteSpace())
                                    {
                                        ToSetValue(item3, e, val, num6);
                                    }
                                }
                            }
                        }
                    }
                }
                FlexSaveService val8 = new FlexSaveService(this.Context, "Save");
                val8.BatchSaveEntityFlexObject(val.BusinessInfo, entity, new DynamicObject[1] { val2.DataEntity });
                foreach (var item6 in dictionary3)
                {
                    val.Model.DeleteEntryRow(text7, item6.Key);
                }
                val.UpdateView(text);
                num2 = num2 + (val.Model.DataObject[text] as DynamicObjectCollection).Count - dictionary3.Count;
            }
        }

        private void ToSetValue(KeyValuePair<string, object> item3, AfterConvertEventArgs e, IBillView val, int num6)
        {
            if (item3.Key == "FSelectMoEntryID")
            {
                DynamicObject moDataForSelectedMo = GetMoDataForSelectedMo(Convert.ToInt64(item3.Value));
                if (e.TargetBusinessInfo.GetForm().Id.EqualsIgnoreCase("PRD_INSTOCK"))
                {
                    val.Model.SetValue("FMoId", moDataForSelectedMo["FID"], num6);
                    val.Model.SetValue("FMoBillNo", moDataForSelectedMo["FBillNo"], num6);
                    val.Model.SetValue("FMoEntryId", moDataForSelectedMo["FEntryId"], num6);
                    val.Model.SetValue("FWorkShopId1", moDataForSelectedMo["FWorkShopID"], num6);
                    val.Model.SetValue("FReqSrc", moDataForSelectedMo["FReqSrc"], num6);
                    val.Model.SetValue("FReqBillNo", moDataForSelectedMo["FSaleOrderNo"], num6);
                    val.Model.SetValue("FReqBillId", moDataForSelectedMo["FSaleOrderId"], num6);
                    val.Model.SetValue("FReqEntryId", moDataForSelectedMo["FSaleOrderEntryId"], num6);
                    val.Model.SetValue("FReqEntrySeq", moDataForSelectedMo["FSaleOrderEntrySeq"], num6);
                    val.Model.SetValue("FSrcBillType", moDataForSelectedMo["PRD_MO"], num6);
                    val.Model.SetValue("FSrcBillNo", moDataForSelectedMo["FBillNo"], num6);
                    val.Model.SetValue("FMOMAINENTRYID", moDataForSelectedMo["FEntryId"], num6);
                    val.Model.SetValue("FKeeperId", moDataForSelectedMo["FStockInOrgId"], num6);
                }
                else if (e.TargetBusinessInfo.GetForm().Id.EqualsIgnoreCase("PRD_MORPT"))
                {
                    val.Model.SetValue("FMoId", moDataForSelectedMo["FID"], num6);
                }
                val.Model.SetValue("FMoBillNo", moDataForSelectedMo["FBillNo"], num6);
                val.Model.SetValue("FMoEntryId", moDataForSelectedMo["FEntryId"], num6);
                val.Model.SetValue("FWorkShipId", moDataForSelectedMo["FWorkShopID"], num6);
                val.Model.SetValue("FReqSrc", moDataForSelectedMo["FReqSrc"], num6);
                val.Model.SetValue("FReqBillNo", moDataForSelectedMo["FSaleOrderNo"], num6);
                val.Model.SetValue("FReqBillId", moDataForSelectedMo["FSaleOrderId"], num6);
                val.Model.SetValue("FReqEntryId", moDataForSelectedMo["FSaleOrderEntryId"], num6);
                val.Model.SetValue("FReqEntrySeq", moDataForSelectedMo["FSaleOrderEntrySeq"], num6);
                val.Model.SetValue("FMOMAINENTRYID", moDataForSelectedMo["FEntryId"], num6);
                val.Model.SetValue("FOwnerId", moDataForSelectedMo["FInStockOwnerId"], num6);
                val.Model.SetValue("FOwnerTypeId", moDataForSelectedMo["FInStockOwnerTypeId"], num6);
                val.Model.SetValue("FStockInOrgId", moDataForSelectedMo["FStockInOrgId"], num6);
                val.Model.SetValue("FStockId", moDataForSelectedMo["FStockId"], num6);
            }
            else
            {
                val.Model.SetValue(item3.Key, item3.Value, num6);
                val.InvokeFieldUpdateService(item3.Key, num6);
            }
        }

        private DynamicObject GetMoDataForSelectedMo(long moEntryId)
        {
            List<string> list = new List<string>();
            list.Add("FID as FId");
            list.Add("FWORKSHOPID as FWorkshopID");
            list.Add("FMATERIALID as FMaterialID");
            list.Add("FPRDORGID as FPrdOrgId");
            list.Add("FBILLNO as FBillNo");
            list.Add("FTREEENTITY_FENTRYID as FEntryId");
            list.Add("FTREEENTITY_FSEQ as FSeq");
            list.Add("FBILLTYPE as FBillType");
            list.Add("FENTRUSTORGID as FEntrustOrgId");
            list.Add("FREQSRC as FReqSrc");
            list.Add("FSALEORDERNO as FSaleOrderNo");
            list.Add("FSALEORDERID as FSaleOrderId");
            list.Add("FSALEORDERENTRYID as FSaleOrderEntryId");
            list.Add("FSALEORDERENTRYSEQ as FSaleOrderEntrySeq");
            list.Add("FInStockOwnerId as FInStockOwnerId");
            list.Add("FInStockOwnerTypeId as FInStockOwnerTypeId");
            list.Add("FSTOCKINORGID as FStockInOrgId");
            list.Add("FSTOCKID as FStockId");
            QueryBuilderParemeter val = new QueryBuilderParemeter();
            val.FormId = ("PRD_MO");
            val.SelectItems = (SelectorItemInfo.CreateItems(list.ToArray()));
            val.FilterClauseWihtKey = ($" FTreeEntity_FEntryId={moEntryId} ");
            QueryBuilderParemeter val2 = val;
            return MFGServiceHelper.GetDynamicObjectCollection(this.Context, val2, null).FirstOrDefault();
        }

        private DynamicObject GetDynamicObjectByFormIdAndId(Context context, string formId, object orgNumber, string where)
        {
            DynamicObject result = null;
            FormMetadata formMetaData = MetaDataServiceHelper.GetFormMetaData(context, formId);
            QueryBuilderParemeter val = new QueryBuilderParemeter();
            val.FormId = (formId);
            List<string> list = new List<string>();
            if (formMetaData.BusinessInfo.MainOrgField != null)
            {
                list.Add($" {formMetaData.BusinessInfo.MainOrgField.Key}.Fnumber='{orgNumber}' ");
            }
            if (!where.IsNullOrEmptyOrWhiteSpace())
            {
                list.Add(where);
            }
            val.FilterClauseWihtKey = (string.Join(" and ", list));
            DynamicObject[] array = BusinessDataServiceHelper.Load(context, formMetaData.BusinessInfo.GetDynamicObjectType(), val);
            if (array.Length != 0)
            {
                result = array.FirstOrDefault();
            }
            return result;
        }

        private string GetCangWeiZhiJi(string formId, string fnumber)
        {
            string result = string.Empty;
            FormMetadata formMetaData = MetaDataServiceHelper.GetFormMetaData(this.Context, formId);
            QueryBuilderParemeter val = new QueryBuilderParemeter();
            val.FormId = formId;
            val.FilterClauseWihtKey = ($" FDOCUMENTSTATUS='C' AND FFORBIDSTATUS='A' AND FNUMBER='{fnumber}' ");
            val.OrderByClauseWihtKey = formMetaData.BusinessInfo.GetForm().PkFieldName;
            DynamicObject[] array = BusinessDataServiceHelper.Load(this.Context, formMetaData.BusinessInfo.GetDynamicObjectType(), val);
            if (array.Length != 0)
            {
                result = array.FirstOrDefault()["Id"].ToString();
            }
            return result;
        }

        private void SelectKey(Dictionary<string, int> dictionary4, DynamicObjectCollection val4, IBillView val)
        {
            for (int num4 = val4.Count - 1; num4 >= 0; num4--)
            {
                string key = val4[num4][text2].ToString();
                if (!dictionary4.ContainsKey(key))
                {
                    dictionary4.Add(key, 1);
                }
                else
                {
                    val.Model.DeleteEntryRow(text7, num4);
                }
            }
        }

        private void SetName(string sourceId, string targetId, int num)
        {
            if (StringUtils.EqualsIgnoreCase(sourceId, "PUR_PurchaseOrder") && StringUtils.EqualsIgnoreCase(targetId, "STK_InStock"))
            {
                text = "InStockEntry";
                text2 = "POORDERENTRYID";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PUR_ReceiveBill") && StringUtils.EqualsIgnoreCase(targetId, "STK_InStock"))
            {
                text = "InStockEntry";
                text2 = "FSLSrcEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PUR_PurchaseOrder") && StringUtils.EqualsIgnoreCase(targetId, "PUR_MRB"))
            {
                text = "PUR_MRBENTRY";
                text2 = "POORDERENTRYID";
                text3 = "STOCKID";
                empty4 = "FSTOCKLOCID";
                text4 = "FSTOCKLOCID";
                text5 = "FLot";
                text6 = "MATERIALID";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PRD_MO") && StringUtils.EqualsIgnoreCase(targetId, "PRD_MORPT"))
            {
                text = "PRD_MORPTENTRY";
                text2 = "MoEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PRD_MORPT") && StringUtils.EqualsIgnoreCase(targetId, "PRD_INSTOCK"))
            {
                text = "Entity";
                text2 = "SrcEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PRD_MO") && StringUtils.EqualsIgnoreCase(targetId, "PRD_INSTOCK"))
            {
                text = "Entity";
                text2 = "SrcEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "SAL_DELIVERYNOTICE") && StringUtils.EqualsIgnoreCase(targetId, "SAL_OUTSTOCK"))
            {
                text = "SAL_OUTSTOCKENTRY";
                text2 = "FHSrcEntryId";
                text3 = "StockID";
                empty4 = "StockLocID";
                text4 = "FStockLocID";
                text5 = "FLot";
                text6 = "MaterialID";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "SAL_SaleOrder") && StringUtils.EqualsIgnoreCase(targetId, "SAL_OUTSTOCK"))
            {
                text = "SAL_OUTSTOCKENTRY";
                text2 = "SOEntryId";
                text3 = "StockID";
                empty4 = "StockLocID";
                text4 = "FStockLocID";
                text5 = "FLot";
                text6 = "MaterialID";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "SAL_RETURNNOTICE") && StringUtils.EqualsIgnoreCase(targetId, "SAL_RETURNSTOCK"))
            {
                text = "SAL_RETURNSTOCKENTRY";
                text2 = "THSrcEntryId";
                text3 = "StockId";
                empty4 = "StocklocId";
                text4 = "FStocklocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PRD_PPBOM") && StringUtils.EqualsIgnoreCase(targetId, "PRD_PickMtrl"))
            {
                text = "Entity";
                text2 = "PPBomEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PRD_PPBOM") && StringUtils.EqualsIgnoreCase(targetId, "PRD_ReturnMtrl"))
            {
                text = "Entity";
                text2 = "PPBOMEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PRD_PPBOM") && StringUtils.EqualsIgnoreCase(targetId, "PRD_FeedMtrl"))
            {
                text = "Entity";
                text2 = "PPBomEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PUR_MRAPP") && StringUtils.EqualsIgnoreCase(targetId, "PUR_MRB"))
            {
                text = "PUR_MRBENTRY";
                text2 = "FTLSrcEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
                num = 2;
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "STK_OEMReceive") && StringUtils.EqualsIgnoreCase(targetId, "STK_OEMInStock"))
            {
                text = "OEMInStockEntry";
                text2 = "RESRCENTRYID";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "STK_TRANSFERAPPLY") && StringUtils.EqualsIgnoreCase(targetId, "STK_TransferDirect"))
            {
                text = "TransferDirectEntry";
                text2 = "FDBSrcEntryId";
                empty = "SrcStockId";
                empty2 = "SrcStockLocId";
                empty3 = "FStockLocId";
                text3 = "DestStockId";
                empty4 = "DestStockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
                num = 2;
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "STK_OutStockApply") && StringUtils.EqualsIgnoreCase(targetId, "STK_MisDelivery"))
            {
                text = "BillEntry";
                text2 = "FCKSrcEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
            else if (StringUtils.EqualsIgnoreCase(sourceId, "PRD_ISSUEMTRNOTICE") && StringUtils.EqualsIgnoreCase(targetId, "PRD_PickMtrl"))
            {
                text = "Entity";
                text2 = "PPBomEntryId";
                text3 = "StockId";
                empty4 = "StockLocId";
                text4 = "FStockLocId";
                text5 = "FLot";
                text6 = "MaterialId";
            }
        }

        private IBillView CreateBillView(string formId)
        {
            FormMetadata val = MetaDataServiceHelper.Load(this.Context, formId, true) as FormMetadata;
            Form form = val.BusinessInfo.GetForm();
            Type type = Type.GetType("Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web");
            IDynamicFormViewService val2 = (IDynamicFormViewService)Activator.CreateInstance(type);
            BillOpenParameter val3 = CreateOpenParameter(val);
            IResourceServiceProvider formServiceProvider = form.GetFormServiceProvider(false);
            val2.Initialize(val3, formServiceProvider);
            return val2 as IBillView;
        }
        private BillOpenParameter CreateOpenParameter(FormMetadata meta)
        {
            Form form = meta.BusinessInfo.GetForm();
            BillOpenParameter val = new BillOpenParameter(form.Id, meta.GetLayoutInfo().Id);
            val.Context = (this.Context);
            val.ServiceName = (form.FormServiceName);
            val.PageId = (Guid.NewGuid().ToString());
            val.FormMetaData = (meta);
            val.Status = 0;
            val.PkValue = null;
            val.CreateFrom = 0;
            val.GroupId = ("");
            val.ParentId = 0;
            val.DefaultBillTypeId = ("");
            val.DefaultBusinessFlowId = ("");
            val.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
            List<AbstractDynamicFormPlugIn> list = form.CreateFormPlugIns();
            val.SetCustomParameter(FormConst.PlugIns, list);
            PreOpenFormEventArgs val2 = new PreOpenFormEventArgs(this.Context, val);
            foreach (AbstractDynamicFormPlugIn item in list)
            {
                item.PreOpenForm(val2);
            }
            if (val2.Cancel)
            {
            }
            return val;
        }
    }
}
